//
//  RBMyCollectVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/15.
//

#import "RBMyCollectVC.h"
#import <UIColor+YYAdd.h>
#import "RBThemeConfig.h"
#import <UIControl+YYAdd.h>
#import "UIView+RBAdd.h"
#import "RBDefine.h"
#import <Masonry.h>
#import "UIButton+RBAdd.h"
#import "RBMyCollectGoodsVC.h"
#import "RBMyCollectGroupVC.h"
#import "RBMyCollectShopVC.h"
#import "RBPager.h"

@interface RBMyCollectVC ()<RBPagerScrollViewDelegate>

@property(nonatomic, strong)RBPagerHeaderView *headerView;
@property(nonatomic, strong)RBPagerScrollView *contentView;
@property(nonatomic, strong)NSMutableDictionary *vcsDicM;

@end

@implementation RBMyCollectVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"我的收藏";
    self.view.backgroundColor = UIColor.whiteColor;
    self.headerView.hidden = NO;
    self.contentView.hidden = NO;
}

- (UIViewController *)getVcWithIndex: (NSInteger)index
{
    UIViewController *vc = self.vcsDicM[@(index)];
    if (!vc)
    {
        if (index == 0)
        {
            vc = RBMyCollectGoodsVC.new;
        }
        else if (index == 1)
        {
            vc = RBMyCollectGroupVC.new;
        }
        else if (index == 2)
        {
            vc = RBMyCollectShopVC.new;
        }
        [self addChildViewController:vc];
        self.vcsDicM[@(index)] = vc;
    }
    return vc;
}

#pragma mark - Network

#pragma mark - Event

#pragma mark - Delegate

- (NSInteger)numberOfPagesInPagerScrollView:(RBPagerScrollView *)pagerScrollView
{
    return self.headerView.model.titles.count;
}

- (UIView *)pagerScrollView:(RBPagerScrollView *)pagerScrollView contentViewAtIndex:(NSInteger)index didEndDecelerating:(BOOL)endDecelerating didEndScrollingAnimation:(BOOL)endScrollingAnimation
{
    return [self getVcWithIndex:index].view;
}

- (void)pagerScrollView:(RBPagerScrollView *)pagerScrollView didScrollToIndex:(float)index
{
    [self.headerView scrollBottomLineToIndex:index];
    [self.headerView setButtonSelectedAtIndex:index];
}

#pragma mark - Setter

#pragma mark - Getter

- (RBPagerHeaderView *)headerView
{
    if (!_headerView)
    {
        self.headerView = ({
            NSArray *titleArray = @[@"商品", @"拼团商品", @"店铺"];
            CGFloat btnWidth = Zoom(100);
            
            RBPagerHeaderView *view = RBPagerHeaderView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.centerX.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(40));
                make.width.mas_equalTo(btnWidth * titleArray.count);
            }];
            __weak typeof(self) weakSelf = self;
            view.model = ({
                RBPagerHeaderModel *model = RBPagerHeaderModel.new;
                model.showSeperator = NO;
                model.buttonWidthStyle = RBPagerButtonWidthConstValue;
                model.headerButtonWidth = btnWidth;
                model.titles = titleArray;
                model.titleSizeForNormalState = Zoom(16);
                model.titleSizeForSelectedState = Zoom(16);
                model.titleColorForNormalState = [RBThemeConfig textLightBlackColor];
                model.titleColorForSelectedState = [RBThemeConfig textBlackColor];
                model.bottomLineWidthStyle = RBPagerBottomLineWidthConstValue;
                model.bottomLineWidth = Zoom(15);
                model.bottomLineHeight = Zoom(4);
                model.bottomLineColor = [UIColor colorWithHexString:@"#518CFF"];
                model;
            });
            [view.bottomLineView rbSetCornerRadius:Zoom(2)];
            view.btnEventBlock = ^(NSInteger index, UIButton *btn) {
                [weakSelf.headerView scrollBottomLineToIndex:index];
                [weakSelf.contentView scrollToPageAtIndex:index animated:NO];
            };
            view;
        });
    }
    return _headerView;
}

- (RBPagerScrollView *)contentView
{
    if (!_contentView)
    {
        self.contentView = ({
            RBPagerScrollView *view = RBPagerScrollView.new;
            view.pagerDelegate = self;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.headerView.mas_bottom).offset(Zoom(13));
            }];
            view;
        });
    }
    return _contentView;
}

- (NSMutableDictionary *)vcsDicM
{
    if (!_vcsDicM)
    {
        self.vcsDicM = ({
            NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
            dicM;
        });
    }
    return _vcsDicM;
}

@end
